<?php

require_once 'Connect.class.php';

class PhanTrang{

	public static $SO_DONG_TREN_MOI_TRANG=10;

	public static $SO_TRANG_HIEN_THI=5;


	//public static function pagingPageUpdate($posPage=1,$itemsPerPage=10){

		//$rangeSelect=array();


	//}

	/*
	$list: la ds can phan trang
	*/

	public static function pagingPage(array $a,$pageNumber=1){

		$rowPerPage=10;

		$newArray=array();	

		$tongHang=count($a);

		$tongTrang=$tongHang/$rowPerPage;

		if ($tongTrang>1){

			$c=$rowPerPage*($pageNumber-1); // pos so ban ghi con lai 10*(2-1)=10
			
			if ($pageNumber<=$tongTrang){
				$s=$rowPerPage*$pageNumber; // 10*2=20
				for($i=$c;$i<$s;$i++){
					$newArray[$i]=$a[$i]; // 10->19
				}
			} else {	
				// phan con lai		
				for($i=$c;$i<$tongHang;$i++){
					$newArray[$i]=$a[$i];
				}
			}

		} else $newArray=$a; 

		return $newArray;

	}
	/*
	$page: so trang hien tai
	$a: mang danh sach
	$arg: mang doi so loc

	*/
		public static function calSumPage($sql){

			$conn=new Connect();

			$conn->Query($sql);
			
		    return $conn->getRS()->num_rows;

		}

		public static function comboLinking($sum_rows,$page,array $arg){

			$link="";	

			$tongTrang=ceil($sum_rows/10);

			if ($tongTrang>1){

			$link.="<select name=\"slpagging\" onchange=\"changePage();\">";

			
			$filter="";

			if (count($arg)>=1){

				foreach ($arg as $key=>$value){

					$filter.="&".$key."=".$value;

				}
			}

			//-----------------------------//
			for ($i=1;$i<=$tongTrang;$i++){
							if ($i==(int)$page){
								$link.="<option value=\"?page=".$i.$filter."\" selected> Trang ".$i." </option>";
							} else {							
								$link.="<option value=\"?page=".$i.$filter."\" > Trang ".$i." </option>";
							}
			}	
			//----------------------------//
			/*
			if ($page<$tongTrang){
					$np=$page+1;
					$link.="<a href=\"index.php?page=".$np."\">[ Next] </a>";
			} else {
					$link.=" [ Next ] ";
			}
				//----------------------------------------//
			$link.="<a href=\"index.php?page=".$tongTrang."\">[Cuối cùng] </a>";
			*/
			}

			$link.="</select>";

			return $link;

		}

		public static function linking($sum_rows,$page,array $filter=array()){

		$link="<div id=\"pagging\">";	

		$tongTrang=ceil($sum_rows/10);

		if ($tongTrang>1){

			$link.="<a href=\"index.php?page=1\">  First Page </a>";

			if ($page >1){	

				$bp=$page-1;

					$link.="<a href=\"index.php?page=".$bp."\">  Previous Page </a>";

			} else {	
				$link.="  First Page   ";			
			}
			//-----------------------------------//
			$pos=1;

			$view=$tongTrang;

			// neu so trang > 5

			if ($tongTrang > 5){				

				$view=$page+4;

				if ($page >= 5){				
					$pos=$page;
					$link.="...";
				} else {
					
				}

				if($tongTrang-$pos<=4){

					$pos=$tongTrang-5;

					$view=$tongTrang;
				}

			}

	
			//-----------------------------------//
			$fil="";
			if (count($filter)>=1){
				foreach ($filter as $key=>$value){
					$fil.="&".$key."=".$value;
				}
			}
			//-----------------------------//
			for ($pos;$pos<=$view;$pos++){
							if ($pos==(int)$page){
								$link.="<a href=\"index.php?page=".$pos.$fil."\" id=\"selection\"> ".$pos." </a>";
							} else {	

								$link.="<a href=\"index.php?page=".$pos.$fil."\" > ".$pos." </a>";
												
								
							}
					}	
				//----------------------------//

			if ($page+4<$tongTrang){
				$link.="...";
			}

			if ($page<$tongTrang){
					$np=$page+1;
					$link.="<a href=\"index.php?page=".$np."\"> Next Page </a>";
			} else {
					$link.="  Next  ";
			}
				//----------------------------------------//
			$link.="<a href=\"index.php?page=".$tongTrang."\">  Last Page  </a> </div>";
		
		}

		return $link;
	}


		public static function linkPage(array $a,$page,array $arg){
		$link="";
		$rows=count($a);
		$tongTrang=ceil($rows/self::$SO_DONG_TREN_MOI_TRANG);
		if ($tongTrang>1){
		$link.="<a href=\"index.php?page=1\">[Đầu tiên] </a>";
		if ($page >1){	

			$prePage=$page-1;

				$link.="<a href=\"index.php?page=".$prePage."\"> [ Back ] </a>";

			} else {
				$link.=" [ Back ] ";
			}
			//-----------------------------------//
			$filter="";
			if (count($arg)>=1){
				foreach ($arg as $key=>$value){
					$filter.="&".$key."=".$value;
				}
			}
			//-----------------------------//
			for ($i=1;$i<=$tongTrang;$i++){
						if ($i==(int)$page){
							$link.="[".$i."]";
						} else {							
							$link.="<a href=\"index.php?page=".$i.$filter."\" >[".$i."] </a>";
						}
				}	
			//----------------------------//
			if ($page<$tongTrang){
				$nextPage=$page+1;
				$link.="<a href=\"index.php?page=".$nextPage."\">[ Next] </a>";
			} else {
				$link.=" [ Next ] ";
			}
			//----------------------------------------//
			$link.="<a href=\"index.php?page=".$tongTrang."\">[Cuối cùng] </a>";	
			return $link;
		}



	}
	

	// ---------------------------------------------------------------//


}


?>